﻿using Framework;
using SchemaMonitor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class temp : System.Web.UI.Page
{
	private class CGuidGroupsList : List<CGuidGroups>
	{
		public CGuidGroupsList(Dictionary<Guid, List<Guid>> d)
		{
			foreach (var i in d)
				this.Add(new CGuidGroups() { Hash = i.Key, Ids = i.Value });
			this.Sort();
		}
	}
	private class CGuidGroups : IComparable<CGuidGroups>
	{
		public Guid Hash;
		public List<Guid> Ids;
		public int Count { get { return Ids.Count; } }

		public int CompareTo(CGuidGroups other)
		{
			var i = other.Count.CompareTo(Count);
			if (i == 0)
				i = other.Hash.CompareTo(Hash);
			return i;
		}
	}



	protected void Page_Load(object sender, EventArgs e)
	{
		string p = CWeb.RequestStr("file");
		if (null != p && p.Length > 0)
		{
			Response.BinaryWrite(File.ReadAllBytes(p));
			Response.End();
		}

		var files =  Directory.GetFiles(@"D:\_dump_photos");
		var dict_md5_files = new Dictionary<Guid, List<string>>(files.Length);
		Parallel.ForEach(files, i =>
		{
			var md5 = CBinary.MD5_(File.ReadAllBytes(i));
			List<string> list = null;

			if (!dict_md5_files.TryGetValue(md5, out list))
			{
				lock (dict_md5_files)
				{
					if (!dict_md5_files.TryGetValue(md5, out list))
					{
						list = new List<string>(1);
						dict_md5_files[md5] = list;
					}
				}
			}
			list.Add(i);
		});
		var kkk = new List<Guid>(dict_md5_files.Keys);
		foreach (var i in kkk)
			if (dict_md5_files[i].Count < 2)
				dict_md5_files.Remove(i);

		long bytes = 0;
		foreach (var i in dict_md5_files)
		{
			var vals = i.Value;
			for (int j = 1; j < vals.Count; j++)
			{
				string s = i.Value[j];
				if (File.Exists(s))
				{
					var by = new FileInfo(s).Length;
					bytes += by;

					File.Delete(i.Value[j]);
					Response.Write(i.Value[j] + " " + CUtilities.FileSize(by) + " <br/>");
				}
			}
		}
		Response.Write("TOTAL CLEANUP: " + CUtilities.FileSize(bytes));


		/*
		foreach (var i in files)
		{
			var md5 = CBinary.MD5_(File.ReadAllBytes(i));
			List<string> list = null;
			if (!dict_md5_files.TryGetValue(md5, out list))
			{
				list = new List<string>(1);
				dict_md5_files[md5] = list;
			}
			list.Add(i);
		}
		*/

		int found = 0;
		int notFound = 0;
		foreach (var i in dict_md5_files)
		{

			var tr = new TableRow();
			tbl.Rows.Add(tr);

			var td = new TableCell();
			tr.Cells.Add(td);


			var div = new Panel();
			td.Controls.Add(div);
			td.Wrap = false;

			div.Style.Add("max-width", "500px");
			div.Style.Add("overflow", "auto");

			long id;
			foreach (var path in i.Value)
			//foreach (var path in dict[i])
			{
				var img = new Image();
				img.ImageUrl = "~/temp.aspx?file=" + HttpUtility.UrlEncode(path);
												   img.ToolTip = path;
												   //img.Width = b.PhotoWidth;
				img.Height = 150;// b.PhotoHeight;
				img.Style.Add("margin-right", "10px");
				div.Controls.Add(img);


				var lbl2 = new Label();
				lbl2.Text = CUtilities.FileSize(path) + " &nbsp; ";
				td.Controls.Add(lbl2);

				lbl2.Text = Path.GetFileName(path);
				lbl2.Style.Add("margin-right", "20px");

				var n = Path.GetFileNameWithoutExtension(path);
				if (long.TryParse(n, out id))
				{
					if (null == CPhoto.Cache.GetById(id))
						notFound++;
					else
						found++;

				}
			}

		}
		Response.Write("Found: " + found.ToString() + ", notfound =" + notFound.ToString() + " => " + CTextbox.SetPercent(100.0 * found / (found + notFound), "n0") + "<br>");


		return;
		/*


		var dict = dict_md5_files; //CProto.Deserialise<Dictionary<Guid, List<string>>>(CBinary.Unzip( File.ReadAllBytes(@"D:\_dump_photos\matchOnBoth_guid_listOfString.gz")));
		var keys = new ArrayList(dict.Keys);

		var dict2 = new Dictionary<Guid, List<Guid>>();
		foreach (var i in dict.Keys)
		{
			var bin = CProto.Serialise(dict[i]);
			var hash = CBinary.MD5_(bin);
			List<Guid> list = null;
			if (!dict2.TryGetValue(hash, out list))
			{
				list = new List<Guid>();
				dict2.Add(hash, list);
			}
			list.Add(i);
		}

		var result = new CGuidGroupsList(dict2);



		foreach (CGuidGroups i in ctrlPaging.Display(result))
		//foreach (Guid i in ctrlPaging.Display(keys))
		{
			//var b = CPhotoHash.Cache.GetById(i);

			var img = new Image();
			//img.ImageUrl = CSitemap.Photo(id);//(i);
			////img.ToolTip = b.PhotoMd5.ToString();
			////img.Width = b.PhotoWidth;
			//img.Height = 150;// b.PhotoHeight;

			//td.Controls.Add(img);

			//var lbl = new Label();
			//lbl.Text = "<br/>" + CUtilities.FileSize(b.PhotoSize);
			//td.Controls.Add(lbl);


			//td = new TableCell();
			tr.Cells.Add(td);

			var div = new Panel();
			td.Controls.Add(div);
			td.Wrap = false;

			div.Style.Add("max-width", "500px");
			div.Style.Add("overflow", "auto");

			foreach (var id in i.Ids)			
			//foreach (var path in dict[i])
			{
				img = new Image();
				img.ImageUrl = CSitemap.Photo(id); //"~/temp.aspx?file=" + HttpUtility.UrlEncode(path);
				//img.ToolTip = path;
				//img.Width = b.PhotoWidth;
				img.Height = 150;// b.PhotoHeight;
				img.Style.Add("margin-right", "10px");
				div.Controls.Add(img);

				var b = CPhotoHash.Cache.GetById(id);
				if (null == b)
					continue;

				var lbl2 = new Label();
				lbl2.Text = CUtilities.FileSize(b.PhotoSize) + " &nbsp; ";
				td.Controls.Add(lbl2);

			}
		}
		*/
	}
}